import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_layout/api/home.dart';
import 'package:flutter_layout/bloc/home/home_cubit.dart';
import 'package:flutter_layout/utils/custom_logger.dart';
import 'package:flutter_layout/utils/local_storage.dart';
import 'package:flutter_layout/utils/secure_storage.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:go_router/go_router.dart';

class Home extends StatelessWidget {
  const Home({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => HomeCubit(),
      child: HomeView(),
    );
  }
}

class HomeView extends StatelessWidget {
  HomeView({super.key});

  final List<String> images = [
    'http://gips1.baidu.com/it/u=436886321,1020119268&fm=3028&app=3028&f=JPEG&fmt=auto?w=1280&h=960',
    'http://gips3.baidu.com/it/u=1327179017,961530922&fm=3028&app=3028&f=JPEG&fmt=auto?w=2560&h=1920',
    'http://gips1.baidu.com/it/u=436886321,1020119268&fm=3028&app=3028&f=JPEG&fmt=auto?w=1280&h=960',
    'http://gips3.baidu.com/it/u=1327179017,961530922&fm=3028&app=3028&f=JPEG&fmt=auto?w=2560&h=1920',
  ];
  late final List<Widget> items = images
      .map((ele) =>
          Image.network(ele, width: double.infinity, fit: BoxFit.cover))
      .toList();

  @override
  Widget build(BuildContext context) {
    final homeCubit = BlocProvider.of<HomeCubit>(context);

    return BlocBuilder<HomeCubit, HomeState>(builder: (context, state) {
      return SingleChildScrollView(
        child: Column(
          children: [
            // 轮播
            CarouselSlider(
                options: CarouselOptions(
                  height: 400.h,
                  autoPlay: true,
                  viewportFraction: 1,
                  autoPlayInterval: Duration(seconds: 3),
                  autoPlayAnimationDuration: Duration(milliseconds: 800),
                ),
                items: items),
            ElevatedButton(
                onPressed: () {
                  getBanner();
                },
                child: Text("点击发送请求")),
            ElevatedButton(
                onPressed: () {
                  context.push('/routerView/123');
                },
                child: Text("跳转传参--路径拼接")),
            ElevatedButton(
                onPressed: () {
                  context.push('/routerView?id=234');
                },
                child: Text("跳转传参--query参数")),
            ElevatedButton(
                onPressed: () {
                  log.e("测试log");
                },
                child: Text("日志输出")),
            ElevatedButton(
                onPressed: () async {
                  final file = await log.readLogsInRange(
                      startTime: DateTime(2025, 10, 16),
                      endTime: DateTime(2025, 10, 16));
                  file.forEach((date, content) {
                    print(content);
                  });
                },
                child: Text("读取日志文件")),
            SizedBox(
              height: 30.h,
            ),
            Text(state.title),
            ElevatedButton(
                onPressed: () {
                  homeCubit.changeIndex('ssssscsdkmkdnvjs');
                },
                child: Text("改变")),
            SizedBox(
              height: 130.h,
            ),
            ElevatedButton(
                onPressed: () {
                  Localstorage.setString('token', 'akk');
                },
                child: Text("缓存数据 token:akk")),
            ElevatedButton(
                onPressed: () {
                  Localstorage.getString('token').then((value) {
                    debugPrint(value);
                  });
                },
                child: Text("获取数据 token:akk")),
            ElevatedButton(
                onPressed: () {
                  Localstorage.remove('token');
                },
                child: Text("清空token缓存")),
            SizedBox(
              height: 30.h,
              child: Text("加密缓存数据"),
            ),
            ElevatedButton(
                onPressed: () {
                  Securestorage.save('token',
                      'x2h5CuEw3DRZyDuD+Pjb/1BM4wAU67GYl5HbYzfxct0+8F5kmyEdnsggZC+XuOT7IlurUORxQ38IC4b9QaGNoFZEFzojLZs2JBpUtCb3+CiL6YeH+1kYGdcHvN/8+cBVqEsvDWu08AGSIHUjW0XvpT1E/Z0GsT+Qp4S3OtOnZAc=');
                },
                child: Text("缓存数据 token:加密akk")),
            ElevatedButton(
                onPressed: () {
                  Securestorage.get('token').then((value) {
                    debugPrint(value);
                  });
                },
                child: Text("获取数据 token:加密akk")),
            ElevatedButton(
                onPressed: () {
                  Securestorage.delete('token');
                },
                child: Text("清空token缓存"))
          ],
        ),
      );
    });
  }
}
